I try to use subscribeToMorefor my graphql query in react-native, but I get following issue:
ERROR: Unhandled GraphQL subscription error [Error: Cannot destructure property 'data' of 'undefined' as it is undefined.]
My subscription looks like
// MESSAGE_SUBSCRIPTION
import { gql } from '@apollo/client'
export const TOPIC_MESSAGE_SUBSCRIPTION = gql`
subscription MessageSubscription($data: SubscriptionInput!) {
messageSubscription(data: $data) {
_id
text
}
}
`
My Component
...
subscribeToMore({
document: MESSAGE_SUBSCRIPTION,
variables: {
data: {
_id: params._id,
},
},
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev
console.log(prev, subscriptionData)
return prev
},
})
...
When I try the subscriton in my playground everthing work fine.
The configuration of my apolloClient where missing the websocket connection.
Also I need to add the authorization as param, to get it as conntext
const wsLink = new WebSocketLink({
uri: Platform.select({
ios: 'ws://localhost:4000/',
android: 'ws://10.0.2.2:4000/',
}),
options: {
reconnect: true,
connectionParams: async () => {
const result = await getToken()
return {
authorization: result ? `Bearer ${result.password}` : '',
}
},
},
})
....
export const client = new ApolloClient({
link: split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},
authLink.concat(wsLink as any),
authLink.concat(httpLink as any)
),
cache: new InMemoryCache(),
})